#include ".\msgsetoption.h"
#include <malloc.h>
#include "MsgOK.h"
#include "MsgError.h"


extern "C"
{
#include <config.h>
#include <epan/prefs.h>
}


MsgSetOption::MsgSetOption(std::string data)
{
	char* tb = (char*)alloca(data.size()+1);

	memcpy(tb,data.c_str(),data.size()+1);
	char* ptr=tb;
	while(*ptr) ++ptr;
	*ptr=':';

	m_str=tb;
}

POutputMessage MsgSetOption::Process(Model * model)
{
	// We have string so we should send it to EPAN.
	size_t m_str_size = m_str.size();

	char* tb=(char*)alloca(m_str_size+1);
	if (strncpy_s(tb, m_str_size+1, m_str.c_str(), m_str_size))
		throw "Cannot not copy the string";
	int code = prefs_set_pref(tb);
	switch(code)
	{
	case PREFS_SET_OK:
		return POutputMessage(new MsgOK());
	case PREFS_SET_SYNTAX_ERR:
		return POutputMessage(new MsgError(ERR_PREFS_SET_SYNTAX_ERR, *model, NULL));
	case PREFS_SET_NO_SUCH_PREF:
		return POutputMessage(new MsgError(ERR_PREFS_SET_NO_SUCH_PREF, *model, NULL));
	case PREFS_SET_OBSOLETE:
		return POutputMessage(new MsgError(ERR_PREFS_SET_OBSOLETE, *model, NULL));
	default:
		return POutputMessage(new MsgError(ERR_INTERNAL_ERROR, *model, NULL));
	}
}
